package com.bankcomm.luafuction;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.keplerproject.luajava.LuaException;
import org.keplerproject.luajava.LuaObject;
import org.keplerproject.luajava.LuaState;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.AbsoluteLayout;
import android.widget.ScrollView;
import android.widget.ViewFlipper;
import com.bankcomm.common.ConfigManager;
import com.rytong.tools.animation.EmpAnimation;
import com.rytong.tools.animation.FlingAnimation;
import com.rytong.tools.animation.FlingAnimation.FlingListener;
import com.rytong.tools.animation.Rotate3dAnimation;
import com.rytong.tools.ui.Component;
import com.rytong.tools.utils.Utils;

public class LuaTransition {

    final public int LINEAR = 0;
    final public int EASE_IN = 1;
    final public int EASE_OUT = 2;
    final public int EASE_INOUT = 3;
    static private LuaObject startfunction_;
    static private LuaObject endfunction_;
    
    private Component mComponent = null;
    private int mAnimationStartPoint = 0;
    private int mAnimationTerminalPoint = 0;

    public LuaTransition() {
        // TODO Auto-generated constructor stub
        try {

            Component.LUASTATE.pushObjectValue(this);
            Component.LUASTATE.setGlobal("luatransition");
            // final String temp =
            // "curve = {linear = luatransition.LINEAR, ease_in = luatransition.EASE_IN, ease_out = luatransition.EASE_OUT, ease_inout = luatransition.EASE_INOUT,};";
            // Component.LUASTATE.LdoString(temp);

            final String luahead = "transition = {}; ";
            final String luafc11 = "function transition:from(object,property) luatransition:from(object,property);  end;";
            final String luafc2 = "function transition:to(object,property,duration) luatransition:to(object,property,duration); end;";
            final String luafc3 = "function transition:setCurve(object,cu) luatransition:setCurve(object,cu); end;";
            final String luafc4 = "function transition:translate(object,x,y,duration) luatransition:translate(object,x,y,duration); end;";
            final String luafc5 = "function transition:translateX(object,x,duration) luatransition:translateX(object,x,duration); end;";
            final String luafc6 = "function transition:translateY(object,y,duration) luatransition:translateY(object,y,duration); end;";
            final String luafc7 = "function transition:scale(object,x,y,duration) luatransition:scale(object,x,y,duration); end;";
            final String luafc8 = "function transition:scaleX(object,x,duration) luatransition:scaleX(object,x,duration); end;";
            final String luafc9 = "function transition:scaleY(object,y,duration) luatransition:scaleY(object,y,duration); end;";
            final String luafc10 = "function transition:alpha(object,alpha,duration) luatransition:alpha(object,alpha,duration); end;";
            final String luafc1 = "function transition:rotate(object,angle,axis,duration)  luatransition:rotate(object,angle,axis,duration); end;";
            final String luafc12 = "function transition:skew(object,xangle, yangle , duration) luatransition:skew(object,xangle, yangle , duration); end;";
            final String luafc13 = "function transition:skewX(object,angle,time) luatransition:skewX(object,angle,time); end;";
            final String luafc14 = "function transition:skewY(object,angle, duration) luatransition:skewY(object,angle, duration); end;";
            final String luafc15 = "function transition:matrix(object,matrixTable , duration) luatransition:matrix(object,matrixTable,duration); end;";
            final String luafc16 = "function transition:setStartListener(object,func)  luatransition:setStartListener(object,func); end;";
            final String luafc17 = "function transition:setStopListener(object,func) luatransition:setStopListener(object,func); end;";
            final String luafc18 = "function transition:pageTransition(page1,page2,animationStyle) luatransition:pageTransition(page1,page2,animationStyle); end;";
            final String luafc19 = "function transition:setRepeatCount(object,time) luatransition:setRepeatCount(object,time); end;";
            final String luafc20 = "function transition:scrollTo(object) luatransition:scrollTo(object); end;";
            Component.LUASTATE.gc(LuaState.LUA_GCCOLLECT, 0);
            int index = Component.LUASTATE.LdoString(luahead.concat(luafc1).concat(luafc2).concat(luafc3)
                    .concat(luafc4).concat(luafc5).concat(luafc6).concat(luafc7).concat(luafc8).concat(luafc9)
                    .concat(luafc10).concat(luafc11).concat(luafc12).concat(luafc13).concat(luafc14).concat(luafc15)
                    .concat(luafc16).concat(luafc17).concat(luafc19).concat(luafc20));
            if (index > 0) {
                Utils.printOutToConsole("Lua Text has some errors:");
                Utils.printOutToConsole(Component.LUASTATE.LcheckString(-1));
            }
        } catch (Exception e) {
            Utils.printException(e);
        }
    }

    /**
     * 
     * @param object
     * @param repeatCount
     *            object：需操作的目标对象； repeatCount：重复次�?-1表示无限循环
     */
    public void setRepeatCount(Object object, int repeatCount) {
        if (object instanceof Component) {
            Animation anim = ((Component) object).getAnimation();
            if (null == anim || !(anim instanceof EmpAnimation)) {
                anim = new EmpAnimation();
            }
            if (repeatCount < 0)
                anim.setRepeatCount(Animation.INFINITE);
            else{
            	repeatCount = --repeatCount < 0 ? 0 : repeatCount;
            	anim.setRepeatCount(repeatCount);
            }
            ((Component) object).setAnimation(anim);
        }
    }

    /**
     * 
     * @param page1
     * @param page2
     * @param animationStyle
     *            page1：即将移除的页面对象 page2：即将显示的页面对象 animationStyle：页面切换动画类???
     */
    public void pageTransition(Component page1, Component page2, Object animationStyle) {
        ViewGroup vg1 = (ViewGroup) page1.realView_.getParent();
        if (null != vg1) {
            vg1.removeView(page1.realView_);
            vg1 = null;
        }
        ViewGroup vg2 = (ViewGroup) page2.realView_.getParent();
        if (null != vg2) {
            vg2.removeView(page2.realView_);
            vg2 = null;
        }
        ViewFlipper vFlipper = new ViewFlipper(ConfigManager.currentView_);
        vFlipper.addView(page1.realView_);
        vFlipper.addView(page2.realView_);
        vFlipper.setInAnimation((Animation) animationStyle);
        vFlipper.setOutAnimation((Animation) animationStyle);
        ConfigManager.currentView_.setContentView(vFlipper);
    }

    public void setStopListener(Object object, final LuaObject function) {
        if (object instanceof Component) {
            Component component = (Component) object;
            if (component.realView_ != null) {
                endfunction_ = function;
            }
        }
    }

    public void setStartListener(Object object, final LuaObject function) {
        if (object instanceof Component) {
            Component component = (Component) object;
            if (component.realView_ != null) {
                startfunction_ = function;
            }
        }
    }

    /**
     * 
     * @param object
     * @param matrixTable
     * 
     *            object：控件对�?matrixTable：table类型，最多有九个元素组成：{m11=0, m12=…�? m33=0}
     *            
     *            CGFloat m11, m12, m13;
     *            CGFloat m21, m22, m23;
     *            CGFloat m31, m32, m33;
     */
    public void matrix(Component object, LuaObject matrixTable , int duration) {

        HashMap resultMap = Utils.getTableValue(matrixTable);
        
        Map<String,Integer> map = Utils.getMatrixIndex();
        
        float[] toMatrix = new float[]{1f,0f,0f,0f,1f,0f,0f,0f,1f};
        
        Iterator itTo= resultMap.keySet().iterator();
    	while(itTo.hasNext()){
    		String key = (String)itTo.next();
    		if(map.containsKey(key)){
    			toMatrix[map.get(key)] = Float.valueOf((String) resultMap.get(key));
        	}
    	}
        
        EmpAnimation eaAnimation = new EmpAnimation();
        eaAnimation.setMatrix(null , toMatrix ,((Component)object).width_ / 2 ,((Component)object).height_ / 2 );
        eaAnimation.setDuration(duration * 1000);
        eaAnimation.setFillAfter(true);
        setListener(object, eaAnimation);
        object.startAnimation(eaAnimation);
    }

    /**
     * 
     * @param object
     * @param yangle
     *            object：控件对�?angle：斜拉角度�?
     */
    public void skewY(Component object, float yangle , int time) {
    	float fromy = (object.animaExeCount_ - 1) * yangle;
    	float toy = object.animaExeCount_ * yangle;
        EmpAnimation eaAnimation = new EmpAnimation();
        eaAnimation.setSkew(true, 0f,0f, fromy,toy , object.width_ / 2 ,object.height_ / 2);
        eaAnimation.setFillAfter(true);
        eaAnimation.setDuration(time * 1000);
        Animation ea  = object.getAnimation();
        if( ea != null && ea instanceof EmpAnimation){
        	eaAnimation.setRepeatCount(ea.getRepeatCount());
        }
        setListener(object, eaAnimation);
        object.startAnimation(eaAnimation);
        if(eaAnimation.getRepeatCount() == 0){
        	object.animaExeCount_++;
        }
    }

    /**
     * 
     * @param object
     * @param angle
     * 
     *            object：控件  xangle：斜拉角度值
     */
    public void skewX(Component object, float xangle, int time) {
    	float fromx = (object.animaExeCount_ - 1) * xangle;
    	float tox = object.animaExeCount_ * xangle;
        EmpAnimation eaAnimation = new EmpAnimation();
        eaAnimation.setFillAfter(true);
        eaAnimation.setSkew(true, fromx ,tox , 0f , 0f , object.width_ / 2 ,object.height_ / 2);
        eaAnimation.setDuration(time * 1000);
        Animation ea  = object.getAnimation();
        if( ea != null && ea instanceof EmpAnimation){
        	eaAnimation.setRepeatCount(ea.getRepeatCount());
        }
        setListener(object, eaAnimation);
        object.startAnimation(eaAnimation);
        if(eaAnimation.getRepeatCount() == 0){
        	object.animaExeCount_++;
        }
    }

    /**
     * 
     * @param object
     * @param xangle
     * @param yangle
     * 
     *            object：控件对�?xangle：x轴方向斜拉角度�?yangle?��y轴方向斜拉角度�?
     */
    public void skew(Component object, float xangle, float yangle , int duration) {
    	float fromx = (object.animaExeCount_ - 1) * xangle;
    	float tox = object.animaExeCount_ * xangle;
    	float fromy = (object.animaExeCount_ - 1) * yangle;
    	float toy = object.animaExeCount_ * yangle;
        
        EmpAnimation eaAnimation = new EmpAnimation();
        eaAnimation.setSkew(true,fromx ,tox , fromy,toy , object.width_ / 2 , object.height_ / 2);
        eaAnimation.setDuration(duration * 1000);
        eaAnimation.setFillAfter(true);
        Animation ea  = object.getAnimation();
        if( ea != null && ea instanceof EmpAnimation){
        	eaAnimation.setRepeatCount(ea.getRepeatCount());
        }
        setListener(object, eaAnimation);
        object.startAnimation(eaAnimation);
        if(eaAnimation.getRepeatCount() == 0){
        	object.animaExeCount_++;
        }
    }

    /**
     * 
     * @param object
     * @param angle
     * 
     *            object：控件对�?angle：旋转角度�?axis：指定旋转的轴（x,y,z�?duration(�?：动画持续时�?
     */
    public void rotate(Component object, float angle, String axis, int duration) {
        float from = (object.animaExeCount_ - 1) * angle;
        float to = object.animaExeCount_ * angle;
        int repeat = 0;
        Animation anim = object.getAnimation();
        if (null != anim)
            repeat = anim.getRepeatCount();
        if (axis.equalsIgnoreCase("z")) {
            RotateAnimation ra = new RotateAnimation(from, to, Animation.RELATIVE_TO_SELF, 0.5f,
                    Animation.RELATIVE_TO_SELF, 0.5f);
            ra.setDuration(duration * 1000);
            if (repeat < 0)
                ra.setRepeatCount(Animation.INFINITE);
            else
                ra.setRepeatCount(repeat);

            setListener(object, ra);
            ra.setFillAfter(true);
            object.startAnimation(ra);

        } else if (axis.equalsIgnoreCase("y")) {
            Rotate3dAnimation ra = new Rotate3dAnimation(from, to, object.getWidth() >> 1, object.getHeight() >> 1, 0,
                    true, "y");
            ra.setDuration(duration * 1000);
            if (repeat < 0)
                ra.setRepeatCount(Animation.INFINITE);
            else
                ra.setRepeatCount(repeat);
            setListener(object, ra);
            ra.setFillAfter(true);
            object.startAnimation(ra);
        } else {
            Rotate3dAnimation ra = new Rotate3dAnimation(from, to, 0, object.getHeight() >> 1, 0, true, "x");
            ra.setDuration(duration * 1000);
            if (repeat < 0)
                ra.setRepeatCount(Animation.INFINITE);
            else
                ra.setRepeatCount(repeat);
            setListener(object, ra);
            ra.setFillAfter(true);
            object.startAnimation(ra);
        }
        // 不是重复执行时， 增加一�?
        if (repeat == 0) {
            object.animaExeCount_++;
        }

    }

    /**
     * 
     * @param object
     * @param alpha
     *            object：控件对�?alpha：透明度（0~1）duration:持续时间(�?;
     */
    public void alpha(Component object, float alpha, int duration) {
    	float to  = (float) Math.pow((double)alpha, (double) object.animaExeCount_);
    	float from = 1.0f;
    	to = to > 1 ? 1 : to;
    	if(object.animaExeCount_ != 1){
    		if(alpha == 0){
    			from = 0.0f;
    		} else {
    			from = to / alpha;
    		}
    	}
        AlphaAnimation aa = new AlphaAnimation(from, to);
        aa.setDuration(duration * 1000);
        /* 设置执行完动画后不恢复原�? */
        aa.setFillAfter(true);
        //设置重复次数
        Animation ea  = object.getAnimation();
        if( ea != null && ea instanceof EmpAnimation){
        	aa.setRepeatCount(ea.getRepeatCount());
        }
        if (null != object.getAnimation() && null != object.getAnimation().getInterpolator())
            aa.setInterpolator(object.getAnimation().getInterpolator());
        setListener(object, aa);
        object.startAnimation(aa);
        if(aa.getRepeatCount() == 0){
        	object.animaExeCount_ ++;
        }
    }

    /**
     * 
     * @param object
     * @param y
     * 
     *            object：控件对�?y：y轴方向放大倍数（高度）
     */
    public void scaleY(Component object, float y, int duration) {
        float from = 1;
        if (object.animaExeCount_ != 1)
            from = (float) Math.pow(y, object.animaExeCount_ - 1);
        float to = (float) Math.pow(y, object.animaExeCount_);
        ScaleAnimation sa = new ScaleAnimation(1, 1, from, to, Animation.RELATIVE_TO_SELF, 1, Animation.RELATIVE_TO_SELF,
                0.5f);
        sa.setDuration(duration * 1000);
        sa.setFillAfter(true);
        Animation ea  = object.getAnimation();
        if( ea != null && ea instanceof EmpAnimation){
        	sa.setRepeatCount(ea.getRepeatCount());
        }
        setListener(object, sa);
        object.startAnimation(sa);
        if(sa.getRepeatCount() == 0){
        	object.animaExeCount_++;
        }
    }

    /**
     * 
     * @param object
     * @param x
     *            object：控件对???x：x轴方向放大倍数�??��度）
     */
    public void scaleX(Component object, float x, int duration) {
        float from = 1;
        if (object.animaExeCount_ != 1)
            from = (float) Math.pow(x, object.animaExeCount_ - 1);
        float to = (float) Math.pow(x, object.animaExeCount_);
        ScaleAnimation sa = new ScaleAnimation(from, to, 1, 1, Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, 1);
        sa.setDuration(duration * 1000);
        sa.setFillAfter(true);
        Animation ea  = object.getAnimation();
        if( ea != null && ea instanceof EmpAnimation){
        	sa.setRepeatCount(ea.getRepeatCount());
        }
        setListener(object, sa);
        object.startAnimation(sa);
        if(sa.getRepeatCount() == 0){
        	object.animaExeCount_++;
        }
    }

    /**
     * 
     * @param object
     * @param x
     * @param y
     *            object：控件对�?x：x轴方向放大倍数（宽度） y：y?��方向放大倍数（高??�） duration(�?：动画持续时�?
     */
    public void scale(final Component object, final float x, final float y, int duration) {
        float toX = (float) Math.pow((double) x, (double) object.animaExeCount_);
        float toY = (float) Math.pow((double) y, (double) object.animaExeCount_);

        // 动画效果持续时间
        int time = duration * 1000;
        ScaleAnimation sa = new ScaleAnimation(toX / x, toX, toY / y, toY, Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f);
        sa.setDuration(time);
        sa.setFillAfter(true);
        Animation ea  = object.getAnimation();
        if( ea != null && ea instanceof EmpAnimation){
        	sa.setRepeatCount(ea.getRepeatCount());
        }
        setListener(object, sa);
        object.startAnimation(sa);
        int repeat = sa.getRepeatCount();
        // 执行次数
        if (repeat == 0) {
            object.animaExeCount_++;
        }
    }

    /**
     * 
     * @param object
     * @param y
     *            object：控件对�?y：y轴偏移量�?
     */
    public void translateY(final Component component, int y, int duration) {
//    	float terminalY = 0;
//    	Animation animation = component.getAnimation();
//    	if (animation instanceof MoveAnimation) {
//    		final MoveAnimation move = (MoveAnimation) animation;
//    		if (!move.hasEnded()) {
//    			return;
//    		}
//    		terminalY = move.getTerminalY();
//    	}
//    	// 动画效果持续时间
//    	int time = duration * 1000;
//    	final int startPoint = Utils.getScaledValueY(terminalY);
//    	final int terminalPoint = Utils.getScaledValueY(terminalY + y);
//    	final MoveAnimation moveAnimation = new MoveAnimation(0, 0, startPoint, terminalPoint);
//    	moveAnimation.setFillAfter(true);
//    	moveAnimation.setDuration(time);
//    	moveAnimation.setTerminal(0, terminalY + y);
//    	setListener(component, moveAnimation);
//    	ConfigManager.currentView_.runOnUiThread(new Runnable() {
//    		public void run() {
//    			component.startAnimation(moveAnimation);
//    		}
//    	});
    	final View view = component.realView_; // 需要移动的对象。
    	final int yi = (int) Utils.getScaledValueY((float)y); // 纵向移动距离。
    	final int aDuration = (int) (duration * 1000); // 持续时间。
    	FlingAnimation fling = new FlingAnimation(view.getContext()); // 定义一个fling动画对象。
    	fling.startFling(view, 0, yi, aDuration, new FlingListener() { // 开始移动。
    		@Override
    		public void offset(View view, int x, int y) {
    			LayoutParams l = view.getLayoutParams();
    			if (l instanceof LayoutParams) {
    				AbsoluteLayout.LayoutParams al = (AbsoluteLayout.LayoutParams) l;
    				al.x += x;
    				al.y += y;
    				view.setLayoutParams(al);
    				view.invalidate();
    			}
    		}
    		public void finish() {
    			setListener(component, null);
    		}
    	});
    	Animation ea = component.getAnimation();
    	if (ea != null && ea instanceof EmpAnimation) {
    		fling.setRepeatCount(ea.getRepeatCount());
    	}
    }

    /**
     * 
     * @param object
     * @param x
     * @param y
     * 
     *            object：控件对�?x：x轴偏移量�?y：y轴偏移量�?
     */
    public void translate(final Component object, final double x, final double y, double duration) {
    	final View view = object.realView_; // 需要移动的对象。
    	final int xi = (int) Utils.getScaledValueX((float)x); // 横向移动距离。
    	final int yi = (int) Utils.getScaledValueY((float)y); // 纵向移动距离。
    	final int aDuration = (int) (duration * 1000); // 持续时间。
    	FlingAnimation fling = new FlingAnimation(view.getContext()); // 定义一个fling动画对象。
    	fling.startFling(view, xi, yi, aDuration, new FlingListener() { // 开始移动。
    		@Override
    		public void offset(View view, int x, int y) {
    			LayoutParams l = view.getLayoutParams();
    			if (l instanceof LayoutParams) {
    				AbsoluteLayout.LayoutParams al = (AbsoluteLayout.LayoutParams) l;
    				al.x += x;
    				al.y += y;
    				view.setLayoutParams(al);
    				view.invalidate();
    			}
    		}
    		public void finish() {
    			setListener(object, null);
    		}
    	});
    	Animation ea = object.getAnimation();
    	if (ea != null && ea instanceof EmpAnimation) {
    		fling.setRepeatCount(ea.getRepeatCount());
    	}
    }
    
    /**
     * 
     * @param object
     * @param x
     *            object：控件对�?x：x轴偏移量；duration(�?：动画�??续时�?
     */
    public void translateX(final Component component, int x, int duration) {
//    	float terminalX = 0; // 最终移动到哪里。
//    	Animation animation = component.getAnimation();
//    	if (animation instanceof MoveAnimation) {
//    		final MoveAnimation move = (MoveAnimation) animation;
//    		if (!move.hasEnded()) {
//    			return;
//    		}
//    		terminalX = move.getTerminalX();
//    	}
//    	// 动画效果持续时间
//    	int time = duration * 1000;
//    	final int startPoint = Utils.getScaledValueX(terminalX);
//    	final int terminalPoint = Utils.getScaledValueX(terminalX + x);
//    	final MoveAnimation moveAnimation = new MoveAnimation(startPoint, terminalPoint, 0, 0);
//    	moveAnimation.setFillAfter(true);
//    	moveAnimation.setDuration(time);
//    	moveAnimation.setTerminal(terminalX + x, 0);
//    	setListener(component, moveAnimation);
//    	ConfigManager.currentView_.runOnUiThread(new Runnable() {
//			public void run() {
//				component.startAnimation(moveAnimation);
//			}
//		});
    	final View view = component.realView_; // 需要移动的对象。
    	final int xi = (int) Utils.getScaledValueX((float)x); // 横向移动距离。
    	final int aDuration = (int) (duration * 1000); // 持续时间。
    	FlingAnimation fling = new FlingAnimation(view.getContext()); // 定义一个fling动画对象。
    	fling.startFling(view, xi, 0, aDuration, new FlingListener() { // 开始移动。
    		@Override
    		public void offset(View view, int x, int y) {
    			LayoutParams l = view.getLayoutParams();
    			if (l instanceof LayoutParams) {
    				AbsoluteLayout.LayoutParams al = (AbsoluteLayout.LayoutParams) l;
    				al.x += x;
    				al.y += y;
    				view.setLayoutParams(al);
    				view.invalidate();
    			}
    		}
    		public void finish() {
    			setListener(component, null);
    		}
    	});
    	Animation ea = component.getAnimation();
    	if (ea != null && ea instanceof EmpAnimation) {
    		fling.setRepeatCount(ea.getRepeatCount());
    	}
    }

    private void setListener(Component component, Animation animation) {
        // TODO Auto-generated method stub
        if (null == animation) {
            animation = new EmpAnimation();
        }
        component.setAnimation(animation);
        animation.setAnimationListener(new AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {
                Object[] objects = {};
                try {
                    if (null != startfunction_)
                        startfunction_.call(objects);
                } catch (LuaException e) {
                    Utils.printException(e);
                }
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                Object[] objects = {};
                try {
                    if (null != endfunction_)
                        endfunction_.call(objects);
                    // 播放完以后清空回调方�?
                    // 以备下次重新赋值调�?
                    startfunction_ = null;
                    endfunction_ = null;
                } catch (LuaException e) {
                    Utils.printException(e);
                }
            }
        });
    }

    /**
     * 
     * @param curve
     */
    public void setCurve(Object object, Object curveObject) {
        Animation animation;
        if (object instanceof Component) {
            Component c = (Component) object;
            animation = c.getAnimation();
            if (null == animation)
                animation = new EmpAnimation();
            int curve = Integer.parseInt(curveObject.toString());
            switch (curve) {
            case LINEAR:
                // 动画匀速播�?
                animation.setInterpolator(new LinearInterpolator());
                break;
            case EASE_IN:
                // 播放速度先慢后快
                animation.setInterpolator(new AccelerateInterpolator());
                break;
            case EASE_OUT:
                // �?放速度先快后慢
                animation.setInterpolator(new DecelerateInterpolator());
                break;
            case EASE_INOUT:
                // 播放速度先慢再快再慢
                animation.setInterpolator(new AccelerateDecelerateInterpolator());
            default:
                break;
            }
            c.setAnimation(animation);
        }
    }

    /**
     * object：需操作的目标对象； transition-property(table)：结束状态集�?duration(�?
     */
    public void to(Object object, LuaObject table, int duration) {
        if (object instanceof Component) {
        	Component com = (Component) object;
        	HashMap resuleMap = Utils.getTableValue(table);

            Animation anim = ((Component) object).getAnimation();
            if (anim instanceof EmpAnimation) {
                EmpAnimation ea = (EmpAnimation) anim;
                ea.toHash_ = resuleMap;
                android.widget.AbsoluteLayout.LayoutParams lp = 
                		(android.widget.AbsoluteLayout.LayoutParams) (((Component) object).getLayoutParams());
                //设置动画
                Set keySet = ea.fromHash_.keySet();
                AnimationSet animaSet = new AnimationSet(true);
                if(keySet.contains("x") && keySet.contains("y")){
                	String fromXs  = String.valueOf(ea.fromHash_.get("x")) == null ? String.valueOf(com.getLeft()) : String.valueOf(ea.fromHash_.get("x"));
                	String toXs = String.valueOf(ea.toHash_.get("x")) == null ? String.valueOf(com.getLeft()) : String.valueOf(ea.toHash_.get("x"));
                	String fromYs = String.valueOf(ea.fromHash_.get("y")) == null ? String.valueOf(com.getTop()) : String.valueOf(ea.fromHash_.get("y"));
                	String toYs = String.valueOf(ea.toHash_.get("y")) == null ? String.valueOf(com.getTop()) : String.valueOf(ea.toHash_.get("y"));
                	
                	//从相对父控件的位置开始
                	float fromX  = Utils.getScaledValueX(Float.parseFloat(fromXs)) - com.getLeft();
                	float toX = Utils.getScaledValueX(Float.parseFloat(toXs)) - com.getLeft();
                	float fromY = Utils.getScaledValueY(Float.parseFloat(fromYs)) - com.getTop();
                	float toY = Utils.getScaledValueY(Float.parseFloat(toYs)) - com.getTop(); 
                	
                	TranslateAnimation ta = new TranslateAnimation(fromX , toX ,fromY , toY );
                	ta.setRepeatCount(ea.getRepeatCount());
                	animaSet.addAnimation(ta);
                }
                if(keySet.contains("width") && keySet.contains("height")){
                     
                	float fromX = (float)Utils.getScaledValueX(Float.parseFloat(
                	String.valueOf(ea.fromHash_.get("width")) == null ? String.valueOf(lp.width): String.valueOf(ea.fromHash_.get("width")))) 
                			/ ((Component) object).getWidth();
                	float toX = (float)Utils.getScaledValueX(Float.parseFloat(
                	String.valueOf(ea.toHash_.get("width")) == null ? String.valueOf(lp.width) : String.valueOf(ea.toHash_.get("width")))) 
                			/ ((Component) object).getWidth();
                     
                	float fromY = (float)Utils.getScaledValueY(Float.parseFloat(
                			String.valueOf(ea.fromHash_.get("height")) == null ? String.valueOf(lp.height) : String.valueOf(ea.fromHash_.get("height"))))
                			/ ((Component) object).getHeight();
                	float toY = (float)Utils.getScaledValueY(Float.parseFloat(
                			String.valueOf(ea.toHash_.get("height")) == null ? String.valueOf(lp.height) : String.valueOf(ea.toHash_.get("height")))) 
                			/ ((Component) object).getHeight();
                     
                    ScaleAnimation sa = new ScaleAnimation(fromX, toX, fromY, toY);
                    sa.setRepeatCount(ea.getRepeatCount());
                    animaSet.addAnimation(sa);
                }
                if(keySet.contains("alpha")){
                	float fromAlpha = Float.parseFloat(String.valueOf(ea.fromHash_.get("alpha")));
                	float toAlpha = Float.parseFloat(String.valueOf(ea.toHash_.get("alpha")));
                	
                	AlphaAnimation aa = new AlphaAnimation(fromAlpha , toAlpha);
                    aa.setRepeatCount(ea.getRepeatCount());
                	animaSet.addAnimation(aa);
                }
                //矩阵动画
                if(keySet.contains("matrix")){
                	Map<String,Integer> map = Utils.getMatrixIndex();
                    //{m11=1,m21=0,m12=0,m22=1} {m11=0.9,m21=1,m12=1,m22=0.9}  
                    Map fromValues = (HashMap)ea.fromHash_.get("matrix");
                    Map toValues = (HashMap)ea.toHash_.get("matrix");
                    
                    //获取from矩阵值
                	float[] fromMatrix = new float[]{1f,0f,0f,0f,1f,0f,0f,0f,1f};
                	Iterator itfrom = fromValues.keySet().iterator();
                	while(itfrom.hasNext()){
                		String key = (String)itfrom.next();
                		if(map.containsKey(key)){
                    		fromMatrix[map.get(key)] = Float.valueOf((String) fromValues.get(key));
                    	}
                	}
                	//获取to矩阵值
                    float[] toMatrix = new float[]{1f,0f,0f,0f,1f,0f,0f,0f,1f};
                    Iterator itTo= toValues.keySet().iterator();
                	while(itTo.hasNext()){
                		String key = (String)itTo.next();
                		if(map.containsKey(key)){
                			toMatrix[map.get(key)] = Float.valueOf((String) toValues.get(key));
                    	}
                	}
                	//设置
                    ea.setMatrix(fromMatrix, toMatrix,((Component)object).width_ / 2 ,((Component)object).height_ / 2 );
                	animaSet.addAnimation(ea);
                }
                
                animaSet.setInterpolator(ea.getInterpolator());
                animaSet.setDuration(duration * 1000);
                animaSet.setFillAfter(true);
                
                ((Component) object).startAnimation(animaSet);
            }
        }
    }

    /**
     * 
     * @param object
     * @param table
     *            object：需操作的目标对象； transition-property(table)：起始状态属性集�?
     */
    public void from(Object object, LuaObject table) {

    	HashMap resuleMap = Utils.getTableValue(table);

        Animation anim = null;
        if (object instanceof Component) {
            anim = ((Component) object).getAnimation();
        }
        if (anim == null || anim instanceof AnimationSet) {
            anim = new EmpAnimation();
            ((Component) object).setAnimation(anim);
        }
        if (anim instanceof EmpAnimation) {
            EmpAnimation ea = (EmpAnimation) anim;
            ea.fromHash_ = resuleMap;
        }
    }
    
	//设置目标控件，使得页面滚动视图滚动到该位置
    public void scrollTo(final Object toObject){
    	if(null == toObject){
    		return ;
    	}
		if (null != Utils.sc_ && Utils.sc_.realView_ != null) {
			new Handler().postDelayed(new Runnable(){
				@Override
				public void run() {
					final ScrollView scrollView = (ScrollView)Utils.sc_.realView_;
					//int height = scrollView.getHeight();
					int[] location1 = new int[2];
					scrollView.getLocationInWindow(location1);
					
					Component cm = (Component)toObject;
					int[] location2 = new int[2];
					cm.realView_.getLocationInWindow(location2);				
					int distance = location2[1] - location1[1];
					scrollView.scrollBy(0, distance);							
				}
			}, 300);
		}
    }
}
